package org.example.emp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.emp.dto.EmployeeDto;
import org.example.emp.mapper.EmployeeMapper;
import org.example.emp.pojo.Attendance;
import org.example.emp.pojo.Employee;
import org.example.emp.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 *EmployeeServiceImpl继承ServiceImpl, EmployeeServiceImpl实现EmployeeService接口
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper , Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 添加员工
     * @param employee 对象参数传递
     * @return
     */
    @Override
    public boolean addEmployee(Employee employee) {
        Employee employee1 = employeeMapper.selectById(employee.getEmployeeId());

        int temp = employeeMapper.insert(employee);
        if(temp != 0){
            return true;
        }
        return false;
    }

    /**
     * 根据员工id删除员工
     * @param employeeId
     * @return
     */
    @Override
    public boolean deleteEmployee(Integer employeeId) {
        int temp = employeeMapper.deleteEmployeeById(employeeId);
        if(temp != 0){
            return true;
        }
        return false;
    }

    /**
     * 修改员工信息
     * @param employee 对象参数传递
     * @return
     */
    @Override
    public boolean updateEmployee(Employee employee) {
        int temp = employeeMapper.updateEmployeeById(employee);
        if(temp != 0){
            return true;
        }
        return false;
    }

    /**
     * 优化：全局懒加载，分步查询：1.员工基础信息 2.该员工相关的考勤信息
     * */
    @Override
    public EmployeeDto getEmployeeById(Integer employeeId) {
        EmployeeDto employeeDto = employeeMapper.getEmployeeById(employeeId);
        return employeeDto;
    }

    @Override
    public List<Employee> getAllEmployees() {
        List<Employee> employeeList = employeeMapper.selectList(null);
       return employeeList;
    }

    @Override
    public boolean updateEmployeeDepartment(List<Integer> employeeIdList, Integer departmentId) {
        //1.根据传回的id集合查询员工信息
        //2.将员工信息的部门id更改为新的部门id
        //3.如果员工状态为异常，就更改为正常状态
        for (Integer employeeId : employeeIdList) {
            Employee employee = employeeMapper.selectById(employeeId);
            if (employee == null) {
                return false;
            }
            employee.setDepartmentId(departmentId);
            if (employee.getStatus() == 0){
                employee.setStatus(1);
            }
            employeeMapper.updateById(employee);
        }
        return true;
    }
//
//
//    @Override
//    public int updateEmployeeById(Employee employee) {return employeeRepository.updateEmpById(employee);}
}
